package com.flyxiaozhu.erp.core.libs;


import com.flyxiaozhu.erp.core.stockrules.IStockRule;

import java.util.HashMap;
import java.util.Map;

public class StockFactory {
    private static final Map<Integer, IStockRule> rules = new HashMap<>();

    public static void init(Map<String, Object> map) throws Exception {
        for (String key : map.keySet()) {
            Object o = map.get(key);
            if (o instanceof IStockRule) {
                if (rules.containsKey(((IStockRule) o).id())) {
                    throw new Exception("发货规则ID冲突：" + o.getClass().getName());
                }
                rules.put(((IStockRule) o).id(), (IStockRule) o);
            }
        }
    }

    public static Map<Integer, IStockRule> getRules() {
        return rules;
    }

    public static IStockRule getRuleInstance(Integer id) {
        return rules.get(id);
    }
}
